using Microsoft.EntityFrameworkCore;

namespace Core.Db.Helper;

public class PaginationList<T> : List<T>
{
    public int TotalPages { get; private set; }
    public int TotalCount { get; private set; }
    public bool HasPrevious => CurrentPage > 1;
    public bool HasNext => CurrentPage < TotalPages;

    public int CurrentPage { get; set; }
    public int PageSize { get; set; }

    public PaginationList(int totalCount, int currentPage, int pageSize, List<T> items)
    {
        CurrentPage = currentPage;
        PageSize = pageSize;
        AddRange(items);
        TotalCount = totalCount;
        TotalPages = (int)Math.Ceiling(totalCount / (double)pageSize);
    }

    public static async Task<PaginationList<T>> CreateAsync(int currentPage, int pageSize, IQueryable<T> result)
    {
        var totalCount = await result.CountAsync();
        //分页
        //1.跳过一定量的数据
        var skip = (currentPage - 1) * pageSize;
        result = result.Skip(skip);
        //2.以 pageSize 为标准显示一定量的数据
        result = result.Take(pageSize);

        var items = await result.ToListAsync();

        return new PaginationList<T>(totalCount,currentPage, pageSize, items);
    }
}